﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

namespace Fly.Net.Static
{
    public abstract class StaticPageBase : Page
    {
        public static ICache DefaultCacher = new MemoryCache();
        public virtual ICache StaticCacher
        {
            get { return DefaultCacher; }
        }

        public static string DefaultVersion = DateTime.Now.ToString();
        public abstract string CacheVersion
        {
            get;
        }

        public virtual string CacheKey
        {
            get { return this.AppRelativeVirtualPath; }
        }

        protected override void OnPreInit(EventArgs e)
        {
            var page = StaticCacher.Get(CacheKey, CacheVersion);
            if (page != null)
            {
                page.ProcessRequest();
            }

            base.OnPreInit(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            var page = new StaticPage(CacheKey, CacheVersion);
            base.Render(page.StartWrite());
            page.EndWrite(StaticCacher);
            page.ProcessRequest();
        }
    }
}
